package table;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.widgets.TextFilter;
import org.uqbar.arena.widgets.TextInputEvent;
import org.uqbar.arena.windows.SimpleWindow;
import org.uqbar.arena.windows.WindowOwner;
import com.uqbar.commons.StringUtils;
import domain.Player;
import domain.extra.FullTableException;

public class AddPlayerToTheTable extends SimpleWindow<Player> {

	public AddPlayerToTheTable(WindowOwner owner) {
		super(owner, new Player());
	}

	@Override
	protected void addActions(Panel mainPanel) {
		Button join = new Button(mainPanel);
		join.setCaption("Join");
		NotNullObservable nameSelected = new NotNullObservable(Player.NAME);
//		NotNullObservable moneySelected = new NotNullObservable(Player.MONEY);
//		join.bindEnabled(moneySelected);
	
		join.bindEnabled(nameSelected);
		join.onClick(new MessageSend(this, "join"));
	}
	
	public void join() {
		this.getModel().join();
		try {
			this.close(); 
			new RouletteWindow(getOwner(), getModel()).open();
		} catch (FullTableException e) {
			showError(e.getMessage());
		}
	}

	
	protected void createMainTemplate(Panel formBuilder) {
		this.setTitle(" The Happy Roulette " );
		this.setTaskDescription(" Enter your data " );
		super.createMainTemplate(formBuilder);
	}
	
	@Override
	protected void createFormPanel(Panel mainPanel) {
		
		Label nameLabel = new Label(mainPanel);
		nameLabel.setText("Name");
		TextBox name = new TextBox(mainPanel);
		name.bindValueToProperty(Player.NAME);
		TextFilter filter = new TextFilter(){

			public boolean accept(TextInputEvent event) {
				return StringUtils.isBlank(event.getInputText()) || !StringUtils.isNumeric(event.getInputText());
			}
									}  ;
		name.withFilter(filter );

		Label moneyLabel = new Label(mainPanel);
		moneyLabel.setText("Money");
		TextBox money = new TextBox(mainPanel);
		money.bindValueToProperty(Player.MONEY);
		TextFilter filter2 = new TextFilter(){

			public boolean accept(TextInputEvent event) {
				return StringUtils.isBlank(event.getInputText()) || StringUtils.isNumeric(event.getInputText());
			}
									}  ;
		money.withFilter(filter2 );
			
		Label chips = new Label(mainPanel);
		chips.bindValueToProperty(Player.CHIPS);	
	}
}

